<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * Class Comment
 * @package app\api\controller
 * 用户评价
 */
class Comment extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];

    /*
     * 查看
     */
    public function index(){
        $comment = new \app\admin\model\Comment();
        $sjid = $this->uid;
        $num = 4;
        $page = input('page');
        $list = $comment->field('order_num,content,stars,image,create_time')->where(['sjid'=>$sjid])->select();
        $starsSum = $comment->where(['sjid'=>$sjid])->sum('stars');
        $count = $comment->where(['sjid'=>$sjid])->count('id');

        foreach ($list as &$row){
            $row['avg']=$starsSum/$count;
            $row['create_time']=date('Y-m-d',$row['create_time']);
        }
        $arr[0]['avg'] = $starsSum/$count;
        array_walk($arr,function($item) use (&$list) {
            array_unshift($list, $item);
        });
        $allpage = ceil($count/$num);

        $data = [
            'list' => $list,
            'page' => $page,
            'allpage' => $allpage
        ];
        $this->ajaxSuccess($data);
    }

    /*
     * 评论提交
     */
    public function addComment(){

        $data['uid'] = $this->uid;           //用户编号
        $data['order_num'] = input('order_num');   //订单号
        $data['content'] = input('content');       //内容
        $data['image'] = input('image');           //图片
        $data['stars'] = input('stars');           //星级
        $data['create_time'] = time();

        $result = db('comment')->insert($data);
        if ($result){
            $this->ajaxSuccess();
        }

    }


}
